#include <QtCore>
#include <QtGui>
#include <QtWebKit>
#include "mainapplication.h"
#include "mainwidget.h"

#include "t_youtube_dump_lib.h"

static void myMessageOutput(QtMsgType type, const char *msg);

int main(int argc, char *argv[])
{
    qInstallMsgHandler(myMessageOutput);
    MainApplication a(argc, argv);

    QString v_video_id;
    if(argc < 2)
    {
        //QMessageBox::information(0,  "yt_download", QString("main(1):%1").arg(argc));
        qDebug() << QString("main(1):%1").arg(argc);
        exit(0); // Fix Me
        //v_video_id = "jSNvhZNYGkI";
    }
    else
    {
        v_video_id = argv[1]; // T-sxSd1uwoU
    }

    QTranslator qtTranslator;
    qtTranslator.load("qt_" + QLocale::system().name(),
                      QLibraryInfo::location(QLibraryInfo::TranslationsPath));
    a.installTranslator(&qtTranslator);

    QTranslator myappTranslator;
    myappTranslator.load("myapp_" + QLocale::system().name());
    a.installTranslator(&myappTranslator);

    //QMessageBox::information(0,  "yt_download", "main(2)");
    qDebug() << "main(2)";

    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    QCoreApplication::setApplicationName("yt_download");
    QCoreApplication::setOrganizationName("akio.miyoshi");
    QCoreApplication::setOrganizationDomain(QCoreApplication::organizationName());

#if 0x1
    // C:\Users\root\AppData\Roaming\akio.miyoshi
    // yt_download.ini
    QSettings settings(
            QSettings::IniFormat, //QSettings::NativeFormat,
            QSettings::UserScope, // QSettings::SystemScope,
            QCoreApplication::organizationName(),
            QCoreApplication::applicationName()
            );
#else
    // HKEY_CURRENT_USER\Software\akio.miyoshi\yt_download
    QSettings settings2(
            QSettings::NativeFormat, //QSettings::NativeFormat,
            QSettings::UserScope, // QSettings::SystemScope,
            QCoreApplication::organizationName(),
            QCoreApplication::applicationName()
            );
#endif
    //settings.setValue("Login/username", "dummy-user1");
    //settings.setValue("Login/password", "dummy-pass1");

    QWebSettings *webSettings = QWebSettings::globalSettings();
    webSettings->setAttribute(QWebSettings::JavascriptEnabled, true);
    webSettings->setAttribute(QWebSettings::PluginsEnabled, true);
    webSettings->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);

    //QMessageBox::information(0,  "yt_download", QString("main(3):%1").arg(v_video_id));
    qDebug() << QString("main(3):%1").arg(v_video_id);

    if(!a.init(v_video_id)) return 1;

    MainWidget w(0, &a);
    w.show();

    return a.exec();
}

#define MSG_UNIT (1024)
static void myMessageOutput(QtMsgType type, const char *msg)
{
    //in this function, you can write the message to any stream!
    switch (type) {
    case QtDebugMsg:
        //fprintf(stderr, "Debug: %s\n", msg);
    {
        size_t v_sizelen = strlen(msg);
        char v_buff[MSG_UNIT+1];
        for(size_t i=0; i<v_sizelen; i+=MSG_UNIT)
        {
            strncpy(v_buff, msg, MSG_UNIT);
            v_buff[MSG_UNIT]= '\0';
            fprintf(stderr, "%s", v_buff);
            fflush(stderr);
            msg += strlen(v_buff);
        }
        fprintf(stderr, "\n");
        fflush(stderr);
    }
        break;
    case QtWarningMsg:
        fprintf(stderr, "Warning: %s\n", msg);
        break;
    case QtCriticalMsg:
        fprintf(stderr, "Critical: %s\n", msg);
        break;
    case QtFatalMsg:
        fprintf(stderr, "Fatal: %s\n", msg);
        abort();
    }
}
